/*******************
This main.c file is for STC15L104E MCU controlling
2 buttons and 3 leds for display

Stay in powerdown mode until button 1's event 
Go to powerdown while 5minutes or button 1 pushdown again
*******************/
#include <reg51.h>
#include <intrins.h>

sbit	LED3=P3^0;
sbit	LED2=P3^2;
sbit	LED1=P3^3;

sbit	ButtonA=P3^4;
sbit	ButtonB=P3^5;

sfr		INT_CLKO=0x8f;
sfr		AUXR=0x8e;
sfr		P3M1=0xb1;
sfr		P3M0=0xb2;
sfr		CLK_DIV=0x97;
sbit INT2 = P3^6;

unsigned char sw=0;
unsigned char count1=0;
unsigned char count2=0;
unsigned int  t_count=0;
unsigned char time_out=0;

#define SysClk	46875L
#define T1MS	(65536-SysClk/1000)
#define T10MS	(65536-SysClk/100)
#define T100MS	(65536-SysClk/10)
#define T1S		(65536-SysClk)

void System_Initial(void);
void INT2_Routine(void);
void INT3_Routine(void);
void delay(void);
void delay1(void);

void main(void)
{
while(1)
	{
	//io intial
	CLK_DIV=0x07;//divide 6MHZ/128=46875hz
	P3M0=0x0d; //setting P3.0,P3.2,P3.3 as output
	P3M1=0x30; //setting P3.4,P3.5 as input
//	ButtonB=1;
//	ButtonA=1;
	LED1=0;
	LED2=0;
	LED3=0;
	time_out=0;

	INT_CLKO=0x10;//enable int2 and close int 3 at the same time.
	TR0=0;//STOP running timer0
	ET0=0;//disable timer0 interrupt
	EA=1;
//////////////
//        INT2 = 1;                   //ready read INT2 port
//       while (!INT2);              //check INT2
//        _nop_();
//        _nop_();
//        INT_CLKO &= ~0x10;          //(EX2 = 0)disable INT2 interrupt and clear INT2 flag
//        INT_CLKO |= 0x10;           //(EX2 = 1)enable INT2 interrupt
//////////////
	PCON=0x02;//power down the mcu, wait for int2 
        _nop_();
        _nop_();


	//timer0 initial	
	AUXR=0x80; //timer0 work in 1T mode
	TMOD=0x00;//set timer0 as mode0 (16bit auto-reload)
	TL0=T1S;//low byte		
	TH0=T1S>>8;//high byte
	TR0=1;//start running timer0
	ET0=1;//enable timer0 interrupt

	INT_CLKO|=0x10;//enable int2
	INT_CLKO|=0x20;//enable int3
	
	t_count=0; //initial time counter
	EA=1;
	P3M0=0x0d; //setting P3.0,P3.2,P3.3 as output
	P3M1=0x30; //setting P3.4,P3.5 as input

	LED1=1;
	LED2=0;
	LED3=1;

	time_out=0;
	while(time_out==0)
	{
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
//	PCON=0x01;//idle the mcu, save power 
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
	}
	}

}



void System_Initial(void)
{
	INT_CLKO|=0x10;//enable int2

	INT_CLKO|=0x20;//enable int3

	AUXR=0x80; //timer0 work in 1T mode
	TMOD=0x00;//set timer0 as mode0 (16bit auto-reload)
	TL0=T100MS;//low byte		
	TH0=T100MS>>8;//high byte
	TR0=1;//start running timer0
	ET0=1;//enable timer0 interrupt
	
	
	t_count=0; //initial time counter
	EA=1;

}



void INT2_Routine(void) interrupt 10
{
	INT_CLKO &= ~0x10;              //(EX2 = 0)disable INT2 interrupt and clear INT2 flag
	if(LED1==0)
		LED1=1;
	else
		{
		LED1=0;
		LED2=0;
		LED3=0;
		time_out=1;
		}
	delay1();
	INT_CLKO &= ~0x10;              //(EX2 = 0)disable INT2 interrupt and clear INT2 flag
//	ButtonA=1;
}


void INT3_Routine(void) interrupt 11
{
	if(LED2==0)
		{
		LED2=1;
		LED3=0;
		}
	else
		{
		LED2=0;
		LED3=1;
		}
	delay();
//	ButtonB=1;	
}


void delay(void)
{
unsigned int	j=0;
unsigned int 	k=0;
	for(k=0;k<6;k++)// k<4 old settings
	{
		for(j=0;j<200;j++)
		{
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		}
	
	}

}


void delay1(void)
{
unsigned int	j=0;
unsigned int 	k=0;
	for(k=0;k<6;k++)// k<4 old settings
	{
		for(j=0;j<50;j++)
		{
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		}
	
	}

}

void tm0_isr(void) interrupt 1 using 1
{
	t_count++;			//timer is set to interrupt every 1 second 
//	LED3^=1;
	if(t_count>=300)	//t_count>=480(8 minutes),   t_count>=300(5 minutes)
	{
	time_out=1;
	t_count=0;
//	LED3^=1;
	/*
		t_count=0;
		if(LED2==0)
		{
			LED2=1;
			LED3=1;
		}
		else
		{
			LED2=0;
			LED3=0;
		}
	*/
	}
}






